package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ExtraParam;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TargetSet;
import reduce.fz.com.reduce.bean.Team;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.custom.MaterialDialog;
import reduce.fz.com.reduce.custom.MaterialDialogBuilder;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.JoinOrQuitTeamEvent;
import reduce.fz.com.reduce.event.PubDynamicEvent;
import reduce.fz.com.reduce.event.PubSubjectEvent;
import reduce.fz.com.reduce.event.TargetSetEvent;
import reduce.fz.com.reduce.fragment.TeamDetailFragment;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.photo.PhotoSelect;
import reduce.fz.com.reduce.util.photo.SelectParam;

/**
 * 小组详情
 */

public class TeamDetailActivity extends BaseToolBarActivity implements View.OnClickListener {
    private Long teamid;
    private Team team;
    private TeamService teamSevice = ServiceBuilder.builderTeamService(null);


    private FrameLayout teamContainer;
    private TeamDetailFragment detailFragment;
    //发布动态
    private TextView mPubDynamic;
    //见证好友
    private TextView mWitnessFriend;
    //发布故事
    private TextView mPubStory;
   private   TeamDetailFragment fragment=null;
    //发布话题
    private TextView mPubSubject;

    private MaterialDialog targetDialog;
    private final static int TARGET_SET = 129;
    private final static int PUB_DYNAMIC = 130;
    private TeamService teamService = ServiceBuilder.builderTeamService(null);


    /**
     * 获取team详情
     *
     * @param teamid
     */
    public void loadTeam(Long teamid) {
        final LoadingDialog dialog = new LoadingDialog(this);
        dialog.show();
        teamService.loadTeamDetail(teamid, new AbResponseListener<Team>(new TypeReference<Result<Team>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<Team> data) {
                dialog.dismiss();
                team = data.getData();
                displayBtn();
                if (team.teamAttach.isJoin)
                    displayPub(true, true, false, false);
                else
                    displayPub(false, false, false, false);
                attachFragment();

            }
        });
    }

    public void displayBtn() {
        /*if (team.teamAttach.isJoin) {
            setRightBtn("退出", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    teamSevice.quitTeam(teamid, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
                    }) {
                        @Override
                        public void error(ServiceException e) {
                        }

                        @Override
                        public void responseInfo(Result<HashMap> data) {
                            team.teamAttach.isJoin = false;
                            EventBus.getDefault().post(new JoinOrQuitTeamEvent(team, JoinOrQuitTeamEvent.QUIT));
                            finish();
                        }
                    });
                }
            });
        }*/
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setToolbarTitle("小组详情");
        setContentView(R.layout.activity_team_detail);
        teamid = getIntent().getLongExtra("teamid", 0);
        initWidget();
        loadData();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void loadData() {
        loadTeam(teamid);
    }


    public void displayPub(Boolean isPub, Boolean isWitness, Boolean isStory, Boolean isSubject) {
        if (isPub)
            mPubDynamic.setVisibility(View.VISIBLE);
        else mPubDynamic.setVisibility(View.GONE);
        if (isWitness)
            mWitnessFriend.setVisibility(View.VISIBLE);
        else mWitnessFriend.setVisibility(View.GONE);
        if (isStory)
            mPubStory.setVisibility(View.VISIBLE);
        else
            mPubStory.setVisibility(View.GONE);
        if (isSubject) {
            mPubSubject.setVisibility(View.VISIBLE);
        } else mPubSubject.setVisibility(View.GONE);


    }


    public void attachFragment() {
         fragment = TeamDetailFragment.create(team);
        getSupportFragmentManager().beginTransaction().replace(R.id.team_detail_container, fragment).commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) return;
        switch (requestCode) {
            case PUB_DYNAMIC:
                Long mekeepdays = data.getLongExtra("mekeepdays", 0);
                team.teamAttach.keepdays = mekeepdays;
                break;


            default:
                break;

        }
    }

    public void onEvent(TargetSetEvent setEvent) {
        TargetSet set=setEvent.targetSet;
        team.teamAttach.targetset = set;
        fragment.addFriend(set.focusUsers);
        attachFragment();
    }




    @Override
    public void initWidget() {
        mPubDynamic = (TextView) findViewById(R.id.tv_pub_dynamic);
        mWitnessFriend = (TextView) findViewById(R.id.tv_witness_friends);
        mPubStory = (TextView) findViewById(R.id.tv_pub_story);
        mPubSubject = (TextView) findViewById(R.id.tv_pub_subject);
        mPubSubject.setOnClickListener(this);
        mPubStory.setOnClickListener(this);
        mPubDynamic.setOnClickListener(this);
        mWitnessFriend.setOnClickListener(this);
        teamContainer = (FrameLayout) findViewById(R.id.team_detail_container);
    }


    /**
     * 发布动态
     */
    public void pubDynamic() {
       /* if (team.teamAttach.targetset == null || TextUtils.isEmpty(team.teamAttach.targetset.desc)) {
            targetDiaglog();
            return;
        }*/
        Intent intent = new Intent(this, VideoRecoderActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("team", team);
        intent.putExtras(bundle);
        startActivityForResult(intent, PUB_DYNAMIC);
    }

    public void forwardTargetSet() {
        Intent intent = new Intent(this, TargetSettingActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("team", team);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    public void targetDiaglog() {
        if (targetDialog != null && targetDialog.isShowing()) return;
        targetDialog = MaterialDialogBuilder.getBuilder(this).content(R.string.target_setting).positiveText(R.string.target_create).negativeText(R.string.target_cancel).callback(new MaterialDialog.ButtonCallback() {
            @Override
            public void onPositive(MaterialDialog dialog) {
                super.onPositive(dialog);
                forwardTargetSet();

            }

            @Override
            public void onNeutral(MaterialDialog dialogInterface) {
                super.onNeutral(dialogInterface);
                targetDialog.dismiss();
            }
        }).build();
        targetDialog.show();
    }

    public void witnessFriends() {
        Intent intent = new Intent(this, WitnessActivity.class);
        intent.putExtra("teamid", teamid);
        startActivity(intent);
    }

    public void pubStory() {
        SelectParam selectParam = new SelectParam();
        selectParam.selectMax = 10;
        selectParam.nextActivity = PubShareStoryActivity.class.getName();
        PhotoSelect select = PhotoSelect.instance(TeamDetailActivity.this, selectParam);
        ExtraParam params = new ExtraParam();
        params.set("teamid", team.teamid + "");
        select.selectPhoto(params);

    }

    public void pubSubject() {
        Intent intent = new Intent(this, TeamSubjectCreateActivity.class);
        intent.putExtra("teamid", teamid);
        startActivity(intent);

    }


    @Override
    public void initListener() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pub_dynamic:
                pubDynamic();
                break;
            case R.id.tv_witness_friends:
                witnessFriends();
                break;
            case R.id.tv_pub_story:
                pubStory();
                break;
            case R.id.tv_pub_subject:
                pubSubject();
                break;
            default:
                break;
        }

    }
}
